package com.sgu6.sgu6_machine.ui.fragment.set;

import android.app.AlertDialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgu6.middleware.util.HexUtil;
import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.event.MillTempEvent;
import com.sgu6.sgu6_machine.ui.views.AmountView;
import com.sgu6.sgu6_machine.util.TempUtil;
import com.sgu6.sgu6_machine.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 警报设置
 */
public class TemperatureF extends BaseF {

    @BindView(R.id.atHTemp)
    AmountView atHTemp;
    @BindView(R.id.atVTemp)
    AmountView atVTemp;
    @BindView(R.id.atOffsetTemp)
    AmountView atOffsetTemp;
    @BindView(R.id.atMillTemp)
    AmountView atMillTemp;

    Map<String, String> defaultValue = new HashMap<>();
    private AlertDialog.Builder dialog;
    private Thread thread;

    public static TemperatureF newInstance() {
        return new TemperatureF();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_temperature, container, false);
        unbinder = ButterKnife.bind(this, v);

        initUI();
        initDialog();
        return v;
    }

    public void initUI() {
        atVTemp.setDefaultValue(MachineContainer.machineConfigDto.verTemp);
        atHTemp.setDefaultValue(MachineContainer.machineConfigDto.horTemp);
        atOffsetTemp.setDefaultValue(MachineContainer.machineConfigDto.offsetTemp);
        atMillTemp.setDefaultValue(MachineContainer.machineConfigDto.millTemp);

        atHTemp.setAmountMaxValue(150);
        atVTemp.setAmountMaxValue(150);
        atOffsetTemp.setAmountMaxValue(100);
        atMillTemp.setAmountMaxValue(200);
//        initTempValue();
    }

    private void initDialog() {
        dialog = new AlertDialog.Builder(getActivity());
        dialog.setTitle("提示");
        dialog.setMessage("是否进行温度标定？");
        dialog.setNegativeButton("取消", (dialog, which) -> {
        });
        dialog.setPositiveButton("确定", (dialog, which) -> SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.标定温度.code())));
    }

    private void initTempValue() {

        thread = new Thread(() -> Objects.requireNonNull(getActivity()).runOnUiThread(() -> {
            defaultValue = TempUtil.getInstance(getActivity()).read();
            if (defaultValue.get("hTemp") == null) {
                atHTemp.setDefaultValue(120);
            } else {
                atHTemp.setDefaultValue(Integer.parseInt(defaultValue.get("hTemp")));
            }
            if (defaultValue.get("vTemp") == null) {
                atVTemp.setDefaultValue(120);
            } else {
                atVTemp.setDefaultValue(Integer.parseInt(defaultValue.get("vTemp")));
            }
        }));

        thread.start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btnSetHorTemp, R.id.btnSetVerTemp, R.id.btnSetOffsetTemp, R.id.btnSetTemp, R.id.btnSetMillTemp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnSetHorTemp:
                String hc = String.format(MachineCmdEnum.设置横封温度.code(), atHTemp.getValue() * 10);
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(hc));
                MachineContainer.machineConfigDto.horTemp = (atHTemp.getValue());
                updateConfig();
                break;
            case R.id.btnSetVerTemp:
                String vc = String.format(MachineCmdEnum.设置纵封温度.code(), atVTemp.getValue() * 10);
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(vc));
                MachineContainer.machineConfigDto.verTemp = (atVTemp.getValue());
                updateConfig();
                break;
            case R.id.btnSetOffsetTemp:
                String oc = String.format(MachineCmdEnum.设置回差温度.code(), atOffsetTemp.getValue() * 10);
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(oc));
                MachineContainer.machineConfigDto.offsetTemp = (atOffsetTemp.getValue());
                updateConfig();
                break;
            case R.id.btnSetMillTemp:
                MachineContainer.machineConfigDto.millTemp = (atMillTemp.getValue());
                updateConfig();
                ToastUtil.showLongToast(getActivity(), "碾米温度设置成功");
                EventBus.getDefault().post(new MillTempEvent(atMillTemp.getValue()));

                break;
            case R.id.btnSetTemp: //标定温度
                dialog.show();
                break;
        }
    }

}
